package com.masterclient.app.businessmodule.minemodule.cash.contract;

import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.businessmodule.minemodule.cash.bean.CashDetailBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.CashInfoBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/10/17
*/
public class CashInfoPresenter extends CashInfoContract.Presenter{

    @Override
    public void getBalanceCashInfo(String id) {
        mView.baseShowPageLoading(false);
        Observable<CashInfoBean> observable = RetrofitUtil.getInstance().getBalanceCashInfo(id);
        Disposable disposable = toSubscribe(observable, new Consumer<CashInfoBean>() {
            @Override
            public void accept(@NonNull CashInfoBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                if("0".equals(bean.getCode())){
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    mView.showData(bean.getData());
                } else {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR,(bean.msg!=null && bean.msg.length() > 0)?bean.msg:WEB_FAILED_STR);
                    ToastUtil.showToast((bean.msg!=null && bean.msg.length() > 0)?bean.msg:WEB_FAILED_STR);
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getBalanceDetail(String id) {
        mView.baseShowPageLoading(false);
        Observable<CashDetailBean> observable = RetrofitUtil.getInstance().getBalanceDetail(id);
        Disposable disposable = toSubscribe(observable, new Consumer<CashDetailBean>() {
            @Override
            public void accept(@NonNull CashDetailBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                if("0".equals(bean.getCode())){
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    mView.showCashDetailData(bean.getData());
                } else {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR,(bean.msg!=null && bean.msg.length() > 0)?bean.msg:WEB_FAILED_STR);
                    ToastUtil.showToast((bean.msg!=null && bean.msg.length() > 0)?bean.msg:WEB_FAILED_STR);
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        mRxManager.add(disposable);
    }


}